import moment from 'moment'
import gl from './global_variable.js'
import axios from 'axios'

const units = ['B', 'KB', 'MB', 'GB']
var wg = {
	hFileSize: function(value) {
		var idx = 0
		while (value > 1024.0) {
			value = value / 1024.0
			idx++
		}
		return Number(value).toFixed(2) + ' ' + units[idx]
	},
	formatDateTime: function(val) {
		return moment(val).format("YYYY-MM-DD HH:mm:ss")
	},
	axiosGet: function(url, success, err, catchFun) {
		var that = this
		success = success || function(){}
		err = err || function(){}
		catchFun = catchFun || err
		catchFun = catchFun || function(){}
		axios.get(gl.cms_args.server_url + url).then(function(response) {
			var res = response.data
			if (res.code === gl.cms_args.success) {
				success.call(that, res)
			} else {
				err.call(that, res)
			}
		}).catch(function(error) {
			catchFun.call(that, error)
		})
	}
}

export default {
	utils: wg
}
